/* 1videoConference -- An open source video conferencing platform.
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using _videoConference.BAL;
using System.Windows.Media.Animation;
using System.Reflection;
using System.ServiceModel;
using NetPeerService;
using System.ComponentModel;
using ZipInfo;

namespace _videoConference
{
    public partial class Loading : System.Windows.Controls.Page,IDisposable
    {
        System.Data.DataSet ds = new System.Data.DataSet();
        IAddPage ia = new IAddPage();
        private bool disposed = false;
        
        Assembly ass = Assembly.GetAssembly(typeof(Loading));

        public IsvcLoginStatusChannel svcLoginStatus;
        public NetPeerClient npcLoginStatus;
        public object objLoginStatus = new LoginClass();

        public  IsvcLoginInfoChannel channelLoginInfo;
        public  NetPeerClient clientLoginInfo;
        public  object objectLoginInfo = null;

        public bool DoLogin = false;
        public delegate void MoveToMeeting();
        public MoveToMeeting m_MoveToMeeting = null;
        public bool strexec = false;
        public delegate void UIDelegate();

        public Loading()
        {
            InitializeComponent();
            UserInfo.MyCurrentPage = "Loading";
            UserInfo.closelogininfo();
            if (UserInfo.strMeetingMode == "Meeting Mode")
            {
                m_MoveToMeeting = new MoveToMeeting(ProcMoveToMeeting);

                #region LoginStatusClient
                ((LoginClass)objLoginStatus).EntsvcJoin += new LoginClass.DelsvcJoin(Loading_EntsvcJoin);
                ((LoginClass)objLoginStatus).EntsvcDoLogin += new LoginClass.DelsvcDoLogin(Loading_EntsvcDoLogin);

                npcLoginStatus = new NetPeerClient();
                svcLoginStatus = (IsvcLoginStatusChannel)npcLoginStatus.OpenClient<IsvcLoginStatusChannel>("net.tcp://" + UserInfo.CreMachName + ":2500/LoginStatus", "LoginStatusMesh", ref objLoginStatus);
                #endregion

                #region LoginInfoClient

                objectLoginInfo = new CsvcLoginInfo();
                clientLoginInfo = new NetPeerClient();
                ((CsvcLoginInfo)objectLoginInfo).EntsvcJoin+=new CsvcLoginInfo.DelsvcJoin(LoginInfo_EntsvcJoin);
                ((CsvcLoginInfo)objectLoginInfo).EntsvcSetMID += new CsvcLoginInfo.DelsvcSetMID(Loading_EntsvcSetMID);
                ((CsvcLoginInfo)objectLoginInfo).EntsvcSetVar += new CsvcLoginInfo.DelsvcSetVar(Loading_EntsvcSetVar);

                
                channelLoginInfo = (IsvcLoginInfoChannel)clientLoginInfo.OpenClient<IsvcLoginInfoChannel>("net.tcp://" + UserInfo.CreMachName + ":2500/LoginInfo", "LoginInfoMesh", ref objectLoginInfo);
                channelLoginInfo.svcJoin(UserInfo.UserName);
                #endregion

            }
            this.ShowsNavigationUI = false;
            try
            {
                DoubleAnimation oLabelAngleAnimation = new DoubleAnimation();
                oLabelAngleAnimation.From = 0;
                oLabelAngleAnimation.To = 360;
                oLabelAngleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 1, 500));
                oLabelAngleAnimation.RepeatBehavior = RepeatBehavior.Forever;
                RotateTransform oTransform = (RotateTransform)Testellipse.RenderTransform;
                oTransform.BeginAnimation(RotateTransform.AngleProperty, oLabelAngleAnimation);
            }

            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--Loading()--:--");
                clsUtility.LogException = exp;
            }
            
            this.Loaded += new RoutedEventHandler(Loading_Loaded);
            this.Unloaded += new RoutedEventHandler(Loading_Unloaded);
            User.UserInfo.EntUriCollectted += new _videoConference.User.UserInfo.DelUriCollectted(UserInfo_EntUriCollectted);
        }

        void Loading_EntsvcSetVar()
        {
                svcLoginStatus.svcJoin(UserInfo.MyRole);                
                User.UserInfo.Initialize();            
        }

        void LoginInfo_EntsvcJoin(string UName)
        {
        }

        void Loading_EntsvcSetMID(string mid)
        {
            
        }

        void ProcMoveToMeeting()
        {
            try
            {
                if (objLoginStatus != null)
                {
                    npcLoginStatus.CloseClient<IsvcLoginStatusChannel>();
                    if (svcLoginStatus.State != CommunicationState.Closed)
                    {
                        svcLoginStatus.Close();
                        svcLoginStatus.Dispose();
                    }
                    objLoginStatus = null;
                }
                NavigationService.GetNavigationService(this).Navigate(new Uri("DesignPage.xaml", UriKind.RelativeOrAbsolute));
            }
            catch (Exception exp)
            {
                //MessageBox.Show("1videoConference--:--Loading.xaml.cs--:--ProcMoveToMeeting()--:--" + exp.Message);
                exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--ProcMovetoMeeting()--:--");
                clsUtility.LogException = exp;
            }
        }

        void UserInfo_EntUriCollectted()
        {
            if (DoLogin && UserInfo.MyCurrentPage == "Loading")
            {
                try
                {
                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, m_MoveToMeeting);
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--UserInfo_EntUriCollected()If--:--");
                    clsUtility.LogException = exp;
                }
            }
            else
            {
                try
                {
                    DoLogin = true;                    
                    UIDelegate uid = new UIDelegate(UIChangeVisibility);
                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, uid);
                }

                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--UserInfo_EntUriCollected()Else--:--");
                    clsUtility.LogException = exp;
                }
            }
        }

        void objSvcZipInfo_EntZipDownloaded()
        {          
            bool ValSetMID = false;
            if (UserInfo.MeetingID != null && UserInfo.MeetingID != "" && UserInfo.MyRole == "Host")
            {               
                    try
                    {
                        lock (this)
                        {
                            channelLoginInfo.svcSetMID(UserInfo.MeetingID);
                        }                       
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--objSvcZipInfo_EntZipDownloaded()1--:--");
                        clsUtility.LogException = exp;
                    }
                
            }
            else if (UserInfo.MeetingID != null && UserInfo.MeetingID != "" && UserInfo.MyRole != "Host")
            {
                try
                {                  
                    svcLoginStatus.svcJoin(UserInfo.MyRole);
                    User.UserInfo.Initialize();
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--objSvcZipInfo_EntZipDownloaded()2--:--");
                    clsUtility.LogException = exp;
                }
            }
            else if (UserInfo.strMeetingMode == "Design Mode")
            {
                try
                {
                    NavigationService.GetNavigationService(this).Navigate(new Uri("DesignPage.xaml", UriKind.RelativeOrAbsolute));
                }
                catch (Exception exp)
                {                    
                    exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--objSvcZipInfo_EntZipDownloaded()3--:--");
                    clsUtility.LogException = exp;
                }
            }
        }

        void Loading_EntsvcDoLogin()
        {
            try
            {
                if (DoLogin && UserInfo.MyCurrentPage == "Loading")
                {
                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, m_MoveToMeeting);                    
                }
                else
                {
                    DoLogin = true;
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("1videoConference--:--Loading.xaml.cs--:--Loading_EntsvcDoLogin()--:--" + exp.Message);
                exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--Loading_EntsvcDoLogin()--:--");
                clsUtility.LogException = exp;
            }
        }

        void Loading_EntsvcJoin(string Role)
        {
        }

        void Loading_Unloaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (UserInfo.strMeetingMode == "Meeting Mode")
                {
                    #region ClosingLoginStatus
                    try
                    {
                        if (objLoginStatus != null)
                        {
                            npcLoginStatus.CloseClient<IsvcLoginStatusChannel>();
                            if (svcLoginStatus.State != CommunicationState.Closed)
                            {
                                svcLoginStatus.Close();
                                svcLoginStatus.Dispose();
                            }
                            objLoginStatus = null;
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--Loading_Unloaded(LoginStatus)--:--");
                        clsUtility.LogException = exp;
                        //MessageBox.Show("loadingpage unloaded: closing client for login status" + exp.Message);
                    }
                    #endregion

                    #region ClosingLoginInfo
                    try
                    {
                        if (objectLoginInfo != null)
                        {
                            if (channelLoginInfo.State != CommunicationState.Closed)
                            {
                                channelLoginInfo.Close();
                                channelLoginInfo.Dispose();
                            }
                            objectLoginInfo = null;
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--Loading_Unloaded(LoginInfo)--:--");
                        clsUtility.LogException = exp;
                        //MessageBox.Show("loadingpage unloaded: closing client for login status" + exp.Message);
                    }
                    #endregion
                }

                m_MoveToMeeting = null;
                this.Dispose(true);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--Loading.xaml.cs--:--Loading_Unloaded()--:--");
                clsUtility.LogException = exp;
            }
        }       

        void Loading_Loaded(object sender, RoutedEventArgs e)
        {
            if (UserInfo.objSvcZipInfo == null)
            {
                UserInfo.objSvcZipInfo = new ZipInfo.CliSvcZipInfo();
                UserInfo.objSvcZipInfo.EntZipDownloaded += new ZipInfo.CliSvcZipInfo.DelZipDownloaded(objSvcZipInfo_EntZipDownloaded);
            }
        }

        void UIChangeVisibility()
        {
            try
            {
                Testellipse.Visibility = Visibility.Collapsed;
                lblMsg.Visibility = Visibility.Collapsed;
                imgWait.Visibility = Visibility.Visible;
                lblstatus.Visibility = Visibility.Visible;
            }
            catch (Exception exp)
            {
                //MessageBox.Show("UIChangeVisibility : " + exp.Message);
            }          
        }

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);            
            GC.SuppressFinalize(this);    
            
        }

         private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                {
                    try
                    {
                        if (UserInfo.objSvcZipInfo != null)
                        {
                            UserInfo.objSvcZipInfo.npcZipInfoSub.CloseClient<IsvcZipInfoSub>();
                            UserInfo.objSvcZipInfo.npcZipInfoMain.CloseClient<IsvcZipInfoMain>();
                            UserInfo.objSvcZipInfo = null;
                            UserInfo.zipextract = false;
                        }
                    }
                    catch (Exception exp)
                    {
                        //MessageBox.Show(exp.Message);
                    }
                }        
                disposed = true;                
            }
            GC.SuppressFinalize(this);
             
        }

        ~Loading()
        {
            Dispose(false);
        }

        #endregion
    }
}